<template>
  <el-main>
    <!-- Main content -->
    <el-form :model="searchForm" :inline="true" size="small">
      <el-form-item prop="collegeName">
        <el-input v-model="searchForm.collegeName" placeholder="请输入学院名称" />
      </el-form-item>
      <el-form-item prop="majorName">
        <el-input v-model="searchForm.majorName" placeholder="请输入专业名称" />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button icon="el-icon-close" style="color:#FF6760" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:majorList:add']" icon="el-icon-plus" type="primary" @click="addBtn">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增弹框 -->
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form ref="addModel" :model="addModel" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item prop="collegeId" label="所属学院">
            <el-select v-model="addModel.collegeId" style="width:100%" placeholder="请选择学院">
              <el-option
                v-for="item in collegeList"
                :key="item.collegeId"
                :label="item.collegeName"
                :value="item.collegeId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="专业名称" prop="majorName">
            <el-input v-model="addModel.majorName" />
          </el-form-item>
          <el-form-item label="序号" prop="orderNum">
            <el-input v-model="addModel.orderNum" />
          </el-form-item>

        </el-form></div>
    </sys-dialog>
    <!-- 表格定义 -->
    <el-table :height="tableHeight" :data="tableList" border stripe>
      <el-table-column prop="majorName" label="专业名称" />
      <el-table-column prop="collegeName" label="所属学院" />
      <el-table-column prop="orderNum" label="序号" />
      <el-table-column v-if="$checkPermission(['sys:majorList:edit', 'ssys:majorList:delete'])" label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button v-permission="['sys:majorList:edit']" type="primary" size="small" icon="el-icon-edit" @click="editBtn(scope.row)">修改</el-button>
          <el-button v-permission="['sys:majorList:delete']" type="danger" size="small" icon="el-icon-close" @click="deleteBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

  </el-main>

</template>

<script>
import SysDialog from '@/components/dialog/SysDialog.vue'
import { getCollegeApi, addMajorApi, editMajorApi, getMajorApi, deleteMajorApi } from '@/api/major'
export default {
  components: {
    SysDialog
  },
  data() {
    return {
      // 表格高度
      tableHeight: 0,
      rules: {
        collegeId: [{
          required: true,
          message: '请选择学院'
        }],
        majorName: [{
          required: true,
          message: '请输入专业名称',
          trigge: 'blur'
        }]

      },
      // 新增表单绑定数据对象
      addModel: {
        majorId: '',
        collegeId: '',
        majorName: '',
        orderNum: '',
        type: ''
      },
      // 弹窗属性
      addDialog: {
        title: '',
        height: 180,
        width: 650,
        visible: false
      },
      // 搜索绑定对象
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        majorName: '',
        collegeName: '',
        total: 0
      },
      // 设置学院数据
      collegeList: [],
      // 获取表格数据
      tableList: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 220
    })
  },
  created() {
    this.getList()
  },
  methods: {
    // 页容量发生变化
    sizeChange(val) {
      this.searchForm.pageSize = val
      this.getList()
    },
    // 当前页发生变化
    currentChange(val) {
      this.searchForm.currentPage = val
      this.getList()
    },
    // 编辑按钮
    editBtn(row) {
      // 清空表单
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      this.$objCopy(row, this.addModel)
      // 设置弹框属性
      this.addModel.type = '1'
      this.addDialog.title = '编辑专业'
      this.addDialog.visible = true
      // 获取学院列表数据
      this.getCollege()
    },
    // 删除按钮
    async deleteBtn(row) {
      const confirm = await this.$myconfirm('您确认删除该条数据吗？')
      if (confirm) {
        const res = await deleteMajorApi({ majorId: row.majorId })
        if (res && res.code === 200) {
          this.$message.success(res.msg)
          this.getList()
        }
      }
    },
    // 获取列表数据
    async getList() {
      const res = await getMajorApi(this.searchForm)
      if (res && res.code === 200) {
        this.tableList = res.data.records
        this.searchForm.total = res.data.total
      }
    },
    onClose() {
      this.addDialog.visible = false
    },
    onConfirm() {
      this.$refs.addModel.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.type === '0') {
            res = await addMajorApi(this.addModel)
          } else {
            res = await editMajorApi(this.addModel)
          }
          if (res && res.code === 200) {
            // 消息提示
            this.$message.success(res.msg)
            // 刷新列表
            this.getList()
            this.addDialog.visible = false
          }
        }
      })
    },
    // 搜索按钮
    searchBtn() {
      this.getList()
    },
    // 重置按钮
    resetBtn() {
      this.searchForm.collegeName = ''
      this.searchForm.majorName = ''
      this.searchForm.currentPage = 1
      this.searchForm.pageSize = 10
      this.getList()
    },
    // 获取学员列表
    async getCollege() {
      const res = await getCollegeApi()
      if (res && res.code === 200) {
        // 设置学院数据
        this.collegeList = res.data
      }
    },
    // 新增按钮
    addBtn() {
      // 清空表单
      if (this.$refs.addModel !== undefined) {
        this.$refs.addModel.resetFields()
        this.$emptyData(this.addModel)
      }
      // 设置弹框属性
      this.addModel.type = '0'
      this.addDialog.title = '新增专业'
      this.addDialog.visible = true
      // 获取学院列表数据
      this.getCollege()
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
